package com.sqly.doc.ble.xty;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.sqly.doc.R;
import com.sqly.doc.util.MyApplication;
import com.sqly.doc.util.ToastUtil;

/**
 * Created by 袁从斌 on 2019-08-05.
 */
public class GlucoseConcentrationDialog extends DialogFragment {

	private EditText mEtLow;
	private EditText mEtHigh;

	private OnDialogClickListener mOnDialogClickListener;


	public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
		mOnDialogClickListener = onDialogClickListener;
	}

	public interface OnDialogClickListener {
		void onCancel();

		void onConfirm(int[] low, int[] high);
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCancelable(false);
	}

	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		Window window = getDialog().getWindow();
		WindowManager.LayoutParams layoutParams = window.getAttributes();
		layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
		layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		layoutParams.gravity = Gravity.CENTER;
		window.setAttributes(layoutParams);

		View view = inflater.inflate(R.layout.dialog_glucose_concentration, container, false);
		mEtLow = view.findViewById(R.id.etLow);
		mEtHigh = view.findViewById(R.id.etHigh);

		view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mOnDialogClickListener != null) {
					mOnDialogClickListener.onCancel();
				}
				dismiss();
			}
		});

		view.findViewById(R.id.tvConfirm).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String h = mEtHigh.getText().toString();
				String l = mEtLow.getText().toString();
				if (TextUtils.isEmpty(h) || TextUtils.isEmpty(l)) {
					ToastUtil.showShortToast(MyApplication.getApp(), getString(R.string.glucose_concentration));
					return;
				}

				int hh;
				int hl;
				int lh;
				int ll;

				int high = Integer.parseInt(h);
				int low = Integer.parseInt(l);
				if (high <= 0 || low <= 0) {
					ToastUtil.showShortToast(MyApplication.getApp(), getString(R.string.glucose_concentration));
					return;
				}
				if (high >= 100) {
					hh = high / 100;
					hl = high % 100;
				} else {
					hh = 0;
					hl = high;
				}
				if (low >= 100) {
					lh = low / 100;
					ll = low % 100;
				} else {
					lh =0;
					ll = low;
				}
				int[] lows = {lh, ll};
				int[] highs = {hh, hl};
				if (mOnDialogClickListener != null) {
					mOnDialogClickListener.onConfirm(lows, highs);
				}
				dismiss();
			}
		});
		return view;
	}
}
